package weibo4j.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import weibo4j.http.Response;
import weibo4j.org.json.JSONArray;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;

public class BusLine extends WeiboResponse implements Serializable {
	private String key_name;
	private String city;
	private String name;
	private String front_name;
	private String terminal_name;
	private double length;
	private int status;
	private String description;
	private int type;
	private String coordinates;
	private List<BusStation> station;
	
	public BusLine()
	{

	}
	public BusLine(Response res)throws WeiboException, JSONException{
		super(res);
		JSONObject json=res.asJSONObject();
		constructJson(json);
	}
	
	public void constructJson(JSONObject json) throws JSONException {
		this.key_name = json.getString("key_name");
		this.city = json.getString("city");
		this.name = json.getString("name");
		this.front_name = json.getString("front_name");			
		this.terminal_name = json.getString("terminal_name");
		this.length = json.getDouble("length");
		this.status = json.getInt("status");
		this.description = json.getString("description");
		this.type = json.getInt("type");
		this.coordinates = json.getString("coordinates");
		
		JSONArray stations = json.getJSONArray("stations");
		int size = stations.length();
		this.station = new ArrayList<BusStation>(size);
		for (int i = 0; i < size; i++) {
			try {
				this.station.add(new BusStation(stations.getJSONObject(i)));
			} catch (WeiboException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}


	public BusLine(JSONObject json)throws WeiboException, JSONException{
		constructJson(json);
	}
	public BusLine(String str) throws WeiboException, JSONException {
		// StatusStream uses this constructor
		super();
		JSONObject json = new JSONObject(str);
		constructJson(json);
	}

	
public String getKeyname() {
	return key_name;
}
public void setKeyname(String kname) {
	this.key_name = kname;
}
public String getCity() {
	return city;
}
public void setCity(String city) {
	this.city = city;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public String getFrontName() {
	return front_name;
}
public void setFrontName(String fname) {
	this.front_name = fname;
}
public String getTerminalName() {
	return terminal_name;
}
public void setTerminalName(String tname) {
	this.terminal_name = tname;
}
public double getLength() {
	return length;
}
public void setLength(double len) {
	this.length = len;
}
public int getStatus() {
	return status;
}
public void setStatus(int status) {
	this.status = status;
}
public String getDescription() {
	return description;
}
public void setDescription(String description) {
	this.description = description;
}
public int getType() {
	return type;
}
public void setType(int type) {
	this.type = type;
}
public String getCoordinates() {
	return coordinates;
}
public void setCoordinates(String coord) {
	this.coordinates = coord;
}
public List<BusStation> getStation() {
	return station;
}
public void setStation(List<BusStation> stations) {
	this.station = stations;
}

@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + ((key_name == null) ? 0 : key_name.hashCode());
	return result;
}
@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	BusLine other = (BusLine) obj;
	if (key_name != other.key_name)
		return false;
	if (city != other.city)
		return false;
	if (name != other.name)
		return false;
	if (front_name != other.front_name)
		return false;
	if (terminal_name != other.terminal_name)
		return false;
	if (length != other.length)
		return false;
	if (status != other.status)
		return false;
	if (description != other.description)
		return false;
	if (type != other.type)
		return false;
	if (coordinates != other.coordinates)
		return false;
	return true;
}

public static BusLineWapper constructWapperLine(Response res) throws WeiboException {
	JSONObject jsonLine = res.asJSONObject(); //asJSONArray();
	JSONArray lines = null;
	try {
		if(!jsonLine.isNull("lines")){				
			lines = jsonLine.getJSONArray("lines");
			int size = lines.length();
			List<BusLine> line = new ArrayList<BusLine>(size);
			for (int i = 0; i < size; i++) {
				line.add(new BusLine(lines.getJSONObject(i)));
			}
			long previousCursor = jsonLine.getLong("previous_curosr");
			long nextCursor = jsonLine.getLong("next_cursor");
			long totalNumber = jsonLine.getLong("total_number");
			String hasvisible = jsonLine.getString("hasvisible");
			return new BusLineWapper(line, previousCursor, nextCursor,totalNumber,hasvisible);
		}else{
			return null;
		}
		
	} catch (JSONException jsone) {
		throw new WeiboException(jsone);
	}
}

@Override
public String toString() {
	return "BusStation [key_name=" + key_name + 
			", city="+ city + 
			", name="+name+
			", front_name=" + front_name + 
			", terminal_name=" + terminal_name + 			
			", length=" + length + 
			", status=" + status + 
			", description=" + description + 
			", type=" + type + 
			", coordinates=" + coordinates + 
			", stations=" + station.toString() +"]";
	}
}
